package com.cap.dreamcircle.View;

import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.widget.ImageView;

import com.cap.dreamcircle.App.App;
import com.cap.dreamcircle.Base.BaseActivity;
import com.cap.dreamcircle.Model.Bean.BaseBean;
import com.cap.dreamcircle.Model.IUserModel;
import com.cap.dreamcircle.Model.Impl.UserModelImpl;
import com.cap.dreamcircle.R;
import com.cap.dreamcircle.Utils.CommonSubscriber;
import com.cap.dreamcircle.Utils.PreferenceUtils;
import com.cap.dreamcircle.Utils.SimpleMediaPlayerUtils;
import com.cap.dreamcircle.Utils.ToastUtils;
import com.cap.dreamcircle.Utils.TransformUtils;
import com.github.sahasbhop.apngview.ApngDrawable;
import com.github.sahasbhop.apngview.ApngImageLoader;
import com.github.sahasbhop.apngview.assist.ApngListener;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.ButterKnife;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;

/**
 * Created by liucxu on 2017/9/26.
 */

public class SplashActivity extends BaseActivity {

    private IUserModel userModel;

    @BindView(R.id.imv_splash)
    ImageView imv_splash;

    private MediaPlayer mp;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            SimpleMediaPlayerUtils.getInstance(SplashActivity.this).releaseMediaPlayer();
            if (!(TextUtils.isEmpty(PreferenceUtils.GetUserToken()) || TextUtils.isEmpty(PreferenceUtils.GetUserId()))) {
                TestTokenValid();
            } else {
                delayTimer(false);
            }
        }
    };

    @Override
    public int getLayoutId() {
        return R.layout.activity_splash;
    }

    @Override
    public void setupView() {
        userModel = new UserModelImpl();
        ButterKnife.bind(this);
    }

    @Override
    public void setupData() {
        String uri = "assets://apng/boot.png";
        ApngImageLoader.getInstance()
                .displayApng(uri, imv_splash,
                        new ApngImageLoader.ApngConfig(1, true, true), new ApngListener() {
                            @Override
                            public void onAnimationEnd(ApngDrawable apngDrawable) {
                                handler.sendMessage(Message.obtain());
                            }
                        });
        Uri uri_web = getIntent().getData();
        if(uri_web!=null){
            String id= uri_web.getQueryParameter("id");
        }
        mp= MediaPlayer.create(SplashActivity.this, R.raw.splash_audio);
        mp.start();
    }

    private void TestTokenValid() {
        userModel.TestTokenValidate().compose(TransformUtils.<BaseBean>defaultSchedulers()).subscribe(new CommonSubscriber<BaseBean>() {
            @Override
            public void onSuccess(BaseBean response) {
                if (0 == response.getCode()) {
                    delayTimer(true);
                }
            }
            @Override
            public void onFailure(BaseBean response) {
                PreferenceUtils.SaveUserToken("");
                PreferenceUtils.SaveUserId("");
                delayTimer(false);
                ToastUtils.showShortToast(SplashActivity.this, response.getDesc());
            }
        });
    }

    private void delayTimer(final boolean toMainActivity) {
        Observable.timer(500, TimeUnit.MILLISECONDS, AndroidSchedulers.mainThread()).subscribe(new Action1<Long>() {
            @Override
            public void call(Long aLong) {
                if (toMainActivity) {
                    Intent intent = new Intent(SplashActivity.this, MainActivity.class);
                    startActivity(intent);
                    App.GetInstance().finishActivity(SplashActivity.this);
                } else {
                    Intent intent = new Intent(SplashActivity.this, DeviceRegisterActivity.class);
                    startActivity(intent);
                    App.GetInstance().finishActivity(SplashActivity.this);
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        if(mp.isPlaying()){
            mp.stop();
        }
        mp.release();//释放资源
        super.onDestroy();
    }
}
